// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/parking/api/ParkCarApi'

import InfoClass from '@api/parking/model/ParkCarSettingBaseReq'
import EditClass from '@api/parking/model/ParkCarSettingReq'
import ChildClass from '@api/parking/model/SetDto'

const CLIENT = CORE.grpcCLIENT(Client)
const CHILDREN = [ChildClass, 'content', 1]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 信息 —————————————————————————————— //

  async getInfo(param, TIP = '查询设置') {
    const DTO = CORE.grpcDTO(InfoClass, param)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryParkCarSetting', DTO)
    return resp.content || []
  },
  transInfo(obj) {
    const getValue = (key, label) => {
      let value
      const matched = obj.find(item => item.key === key)
      if (matched) {
        if (!isNaN(+matched.value)) value = +matched.value
        Object.assign(matched, { label, value: value ? `${value}` : '', type: 2 })
      } else {
        obj.push({ key, label, value: '', type: 2 })
      }
      return value
    }
    return {
      month: getValue('month', '自动加入黑名单几个月内'),
      count: getValue('count', '自动加入黑名单大于等于多少次'),
      content: obj,
    }
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  transEdit(one, { state: { info } }) {
    return info
  },
  async edit(one, TIP = '修改设置') {
    const DTO = CORE.grpcDTO(EditClass, one, CHILDREN)
    await CORE.grpc(TIP, CLIENT, 'setParkCarSetting', DTO)
    return CORE.grpcOK(TIP)
  },
}
